<?php

namespace app\api\controller\pub;

use app\api\controller\Base;
use app\common\model\pub\Telbind;
use think\facade\Request;
use app\api\controller\Pub;

class Yhpcsoftasr extends Base
{
    /**
     * http://api.yhpcsoft.com/asr/phone
     * 云客 - 电话管理系统
     * 前置：创建数据库：表名[xxx_tel_bind],包含[phone]字段
     * 1、查询绑定[checkBind]：如果data为空证明已绑定，跳到第4步，
     * 2、获取验证码[imgCode]
     * 3、绑定手机号为坐席[bindPhone]
     * 4、请求回拨[dial]
     */
    private $app_key = '';
    private $partnerId = '';
    private $company = '';


    public function initialize()
    {
        parent::initialize();
        // if(!get_config_value('yunke_switch')){
        //     return serr('电话管理系统功能暂未开放');
        // }
        // $this->app_key = get_config_value('yunke_app_key');
        // $this->partnerId = get_config_value('yunke_partnerId');
        // $this->company = get_config_value('yunke_company');
    }
   

    /**
     * 根据手机号获取用户数据
     * @return [type] [description]
     */
    public function phone($url){
        $reqdata = [
            'url' => $url,
        ];
        $req = $this->request('/asr/phone',$reqdata);
        if ($req['code']==false) {
            return serr($req['msg']);
        }
        return sok($req['msg']);
    }


    public function request($path, $data = [],$surl='')
    {
        if(empty($surl)){
            $url = 'http://api.yhpcsoft.com';
        }else{
            $url = $surl;
        }
        $url = $url . $path;
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        if ($data) {
            $jsonData = json_encode($data);
            // 设置请求头，包括 Content-Type
            $header[] = 'Content-Type: application/json';
            $header[] = 'Content-Length: ' . strlen($jsonData);
            curl_setopt($curl, CURLOPT_POST, true);
            curl_setopt($curl, CURLOPT_POSTFIELDS, $jsonData);
        }
        curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        $response = curl_exec($curl);
        $code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
        curl_close($curl);
        if (!$response || $code != 200) {
            //错误强制返回
            return serr($response);
        }
        return sok(json_decode($response, true));
    }
}